package services.application;

import com.avaje.ebean.Ebean;
import dto.app.*;
import models.application.*;
import models.pubobj.*;
import models.user.*;
import play.db.ebean.*;
import services.exception.*;
import utils.*;

import java.util.*;

/**
 * User: yeh
 * Date: 1/27/13
 * Time: 11:23 AM
 * For:  wilddragon
 */
public class AniAppEditService extends  AniAppService{
    public static void updateApp(Map<String, String> appUpdateInfoArgs, AniUser curUser){
        MVCUtils.verifyRequiredElements(appUpdateInfoArgs, UploadAppInfoDto.requiredUpdateFieldKeys);
        UploadAppInfoDto oneAppInfo = new UploadAppInfoDto(appUpdateInfoArgs, curUser);
        checkAppUpdatePrivacy(oneAppInfo, curUser);
        modifyAppInfo(oneAppInfo);
    }

    public static void deleteApp(Map<String, String> appDeleteInfoArgs, AniUser curUser){
        MVCUtils.verifyRequiredElements(appDeleteInfoArgs, UploadAppInfoDto.requiredUpdateFieldKeys);
        UploadAppInfoDto oneAppInfo = new UploadAppInfoDto(appDeleteInfoArgs, curUser);
        checkAppUpdatePrivacy(oneAppInfo, curUser);
        deleteApp(oneAppInfo);
    }

    public static void switchAppEnable(Long appId, AniUser curUser){
        UploadAppInfoDto oneAppInfo = new UploadAppInfoDto(appId, curUser);
        checkAppUpdatePrivacy(oneAppInfo, curUser);

        AniApp srcAppDao = AniAppService.getApp(oneAppInfo.getAniAppDao());
        AniObjectState srcState = srcAppDao.state;
        AniObjectState targetState = null;
        if(AniObjectState.States.ACTIVATED.name().equals(srcState.stateKey)){
            targetState = AniObjectState.findByStateKey(AniObjectState.States.DISABLED.name());
        }else if(AniObjectState.States.DISABLED.name().equals(srcState.stateKey)){
            targetState = AniObjectState.findByStateKey(AniObjectState.States.ACTIVATED.name());
        }
        changeAppState(oneAppInfo, targetState);
    }

    // -- check update privacy
    private static void checkAppUpdatePrivacy(UploadAppInfoDto oneAppInfo, AniUser curUser){
        AniApp oneApp = oneAppInfo.getAniAppDao();
        checkAppExistence(oneApp);
        checkAppOwner(curUser, oneApp);
    }

    // -- Transmission with DB
    @Transactional
    private static void modifyAppInfo(UploadAppInfoDto oneApp){
        try{
            oneApp.getAniAppDao().update();
        }catch(Exception e){
            throw new AniDataException("APP_INFO_MODIFY_FAILED");
        }
    }
    private static void deleteApp(UploadAppInfoDto oneApp){

        changeAppState(
                oneApp,
                AniObjectState.findByStateKey(AniObjectState.States.RETIRED.name())
        );
    }
    @Transactional
    private static void changeAppState(UploadAppInfoDto oneApp, AniObjectState targetState){
        try{
            AniApp curApp = AniAppService.getApp(oneApp.getAniAppDao());
            curApp.state = targetState;
            curApp.update();
        }catch(Exception e){
            e.printStackTrace();
            throw new AniDataException("APP_STATE_CHANGE_FAILED");
        }
    }
}